package com.baibei.ebec.user.address;

import android.content.Context;
import android.text.TextUtils;

import com.baibei.basic.BasicPresenterImpl;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.IUserApi;
import com.baibei.model.AddressInfo;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.Empty;
import com.rae.swift.Rx;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Action;
import io.reactivex.functions.Function;


/**
 * 收货地址
 * Created by ChenRui on 2016/8/13 0013.
 */
public class AddressListPresenterImpl extends BasicPresenterImpl<AddressListContract.View> implements AddressListContract.Presenter {

    private IUserApi mUserApi;


    public AddressListPresenterImpl(Context context, AddressListContract.View view) {
        super(context, view);
        mUserApi = ApiFactory.getInstance().getUserApi();
    }

    @Override
    public void start() {
        super.start();
        if (!isLogin()) {
            mView.onNotLogin();
            return;
        }

        createObservable(mUserApi.getAddress("ALL"))
                .subscribe(new ApiDefaultObserver<List<AddressInfo>>() {
                    @Override
                    protected void onError(String msg) {
                        mView.onEmptyAddress(msg);
                    }

                    @Override
                    protected void accept(List<AddressInfo> data) {
                        if (Rx.isEmpty(data)) {
                            mView.onEmptyAddress(null);
                            return;
                        }
                        mView.onLoadAddress(data);
                    }
                });

    }

    @Override
    public void updateDefaultAddress(String addressId) {
        if (TextUtils.isEmpty(addressId)) {
            mView.onUpdateAddressFailed("获取地址信息错误，请退出重新填写。");
            return;
        }
        // TODO 更新默认地址
        mView.showLoading();
        createObservable(mUserApi.setAddressDefault(addressId)).flatMap(new Function<Empty, Observable<List<AddressInfo>>>() {

            @Override
            public Observable<List<AddressInfo>> apply(@NonNull Empty empty) throws Exception {
                //更新成功，重新刷新界面
                return createObservable(mUserApi.getAddress("ALL"));
            }
        }).doFinally(new Action() {
            @Override
            public void run() throws Exception {
                mView.hideLoading();
            }
        }).subscribe(new ApiDefaultObserver<List<AddressInfo>>() {
            @Override
            protected void onError(String msg) {
                mView.onUpdateAddressFailed(msg);
            }

            @Override
            protected void accept(List<AddressInfo> data) {
                if (Rx.isEmpty(data)) {
                    mView.onEmptyAddress(null);
                    return;
                }
                mView.onLoadAddress(data);
            }
        });
    }

    @Override
    public void deleteAddress(String addressId) {
        if (TextUtils.isEmpty(addressId)) {
            mView.onDeleteAddressFailed("获取地址信息错误，请退出重新填写。");
            return;
        }

        // TODO:删除地址
        mView.showLoading();
        createObservable(mUserApi.deleteAddress(addressId)).flatMap(new Function<Empty, Observable<List<AddressInfo>>>() {

            @Override
            public Observable<List<AddressInfo>> apply(@NonNull Empty empty) throws Exception {
                //更新成功，重新刷新界面
                return createObservable(mUserApi.getAddress("ALL"));
            }
        }).doFinally(new Action() {
            @Override
            public void run() throws Exception {
                mView.hideLoading();
            }
        }).subscribe(new ApiDefaultObserver<List<AddressInfo>>() {
            @Override
            protected void onError(String msg) {
                mView.onDeleteAddressFailed(msg);
            }

            @Override
            protected void accept(List<AddressInfo> data) {
                if (Rx.isEmpty(data)) {
                    mView.onEmptyAddress(null);
                    return;
                }
                mView.onLoadAddress(data);
            }
        });
    }
}
